//
//  BuyNowVC.m
//  reSearchDemo
//
//  Created by 云库 on 15/9/24.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "BuyNowVC.h"
#import "BuyNowCell.h"
#import "GoodsCarList.h"
@interface BuyNowVC ()<UITableViewDataSource,UITableViewDelegate> {
    UITableView *_tableView;   /**< 列表 */
    NSMutableArray *_dataSource;  /**< 数据源 */
    UILabel *moneyLb;            /**< 显示金额label */
    NSMutableArray *_titleArr;    /**< 存放参数数据  */
}
/** 购买数量Lable*/
@property (weak, nonatomic) IBOutlet UILabel *buyNumLable;
/** 购买数量*/
@property (nonatomic,assign) int buyNum;

@end

@implementation BuyNowVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.buyNum = 1;
    
    self.title = @"确认订单";
    
    [self loadDataSource];
    [self allocTableView];
}

- (void)loadDataSource {
    _titleArr = [NSMutableArray array];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"配送方式" forKey:@"title"];
    [dic setObject:@"快递 免邮" forKey:@"Text"];
    NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
    [dic1 setObject:@"配送时间" forKey:@"title"];
    [dic1 setObject:@"周末配送" forKey:@"Text"];
    NSMutableDictionary *dic2 = [NSMutableDictionary dictionary];
    [dic2 setObject:@"可用积分:0" forKey:@"title"];
    [dic2 setObject:@"" forKey:@"Text"];
    [_titleArr addObject:dic];
    [_titleArr addObject:dic1];
    [_titleArr addObject:dic2];
    
    _dataSource = [NSMutableArray arrayWithArray:self.model];
}
- (void)allocTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64,CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - 108) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:235/255.0 alpha:0.9];
    [self.view addSubview:_tableView];
    
    CGFloat price = 0;
    for (int i = 0; i<self.model.count; i++) {
        GoodsCarList *goods = self.model[i];
        price += goods.money*goods.num;
    }
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.view.frame) - 44, CGRectGetWidth(self.view.frame), 44)];
    footView.backgroundColor = [UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:0.9];
    NSMutableAttributedString *Price  = [self ModifyString:[NSString stringWithFormat:@"%.2f",price]];
    moneyLb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(footView.frame)-90, CGRectGetHeight(footView.frame))];
    moneyLb.attributedText = Price;
    moneyLb.textAlignment = NSTextAlignmentRight;
    
    
    UIButton *SureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    SureBtn.frame = CGRectMake(CGRectGetWidth(footView.frame)-80, 0, 80, CGRectGetHeight(footView.frame));
    [SureBtn setTitle:@"确定" forState:UIControlStateNormal];
    SureBtn.backgroundColor = [UIColor orangeColor];
    [SureBtn addTarget:self action:@selector(SureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [footView addSubview:moneyLb];
    [footView addSubview:SureBtn];
    
    [self.view addSubview:footView];
    
}
#pragma mark --获取自定义字符串--
- (NSMutableAttributedString *)ModifyString:(NSString *)Str {
    NSString *price = [NSString stringWithFormat:@"合计:￥%@",Str];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:price];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(3,price.length - 3)];
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:13.0] range:NSMakeRange(price.length - 2, 2)];
    
    return str;
}

- (void)SureBtnAction:(UIButton *)sender {
    DLog(@"确认付款");
}

/** 减号*/
- (IBAction)decreaseBtn:(id)sender {
    if (self.buyNum>1) {
        self.buyNum--;
    }
    
    self.buyNumLable.text = [NSString stringWithFormat:@"%d",self.buyNum];
}

/** 加号*/
- (IBAction)addBtn:(id)sender {
    self.buyNum++;
    self.buyNumLable.text = [NSString stringWithFormat:@"%d",self.buyNum];
    
}

#pragma mark -
#pragma mark --tableViewDelegate--
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.model.count + 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else {
        return 1 +_titleArr.count;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *indentifier = @"BuyNowcell";
    static NSString *indentifier1 = @"cell";
    static NSString *indentifier2 = @"addressCell";
    
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier2];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:indentifier2];
        }
        //防止重复添加控件
        if (cell.subviews.count < 2) {
            UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 60, 20)];
            nameLabel.text = @"收货人:";
            UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(nameLabel.frame), CGRectGetMinY(nameLabel.frame), 80, 20)];
            name.text = @"高富帅";
            UILabel *phone = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame) - 130, CGRectGetMinY(name.frame), 100, 20)];
            phone.text = @"12345678901";
            phone.font = [UIFont systemFontOfSize:13];
            UILabel *address = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(nameLabel.frame), CGRectGetMaxY(nameLabel.frame), CGRectGetWidth(self.view.frame)-CGRectGetMinX(nameLabel.frame) - 30, 40)];
            address.text = [NSString stringWithFormat:@"收货地址:%@",@"广东省广州市白云区黄石区江夏北一路强生大厦A栋408"];
            address.font = [UIFont systemFontOfSize:12];
            address.numberOfLines = 0;
            [cell.contentView addSubview:nameLabel];
            [cell.contentView addSubview:name];
            [cell.contentView addSubview:phone];
            [cell.contentView addSubview:address];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        return cell;
        
    }else if (indexPath.row == 0 && indexPath.section > 0) {
        BuyNowCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
        if (!cell) {
            cell = [[BuyNowCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
        }
        cell.model = self.model[indexPath.section-1];
        return cell;
    }else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier1];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:indentifier1];
        }
        NSMutableDictionary *dic = _titleArr[indexPath.row - 1];
        cell.textLabel.text = dic[@"title"];
        cell.detailTextLabel.text = dic[@"Text"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
//    __block NSMutableArray *arr = self.model;
//    cell.block = ^(NSInteger number) {
//        self.buyNum = (int)number;
//        CGFloat price = 0;
//        for (int i = 0; i<arr.count; i++) {
//            GoodsCarList *goods = arr[i];
//            CGFloat money = number*goods.money;
//            price += money;
//        }
//        NSMutableAttributedString *TPrice = [self ModifyString:[NSString stringWithFormat:@"%.2f",price]];
//        moneyLb.attributedText = TPrice;
//    };
    
    
}
#pragma mark --cell高度--
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 60;
    }else if (indexPath.row == 0) {
        return 120;
    }else {
      return 44;
    }
    
}
#pragma mark --cell点击方法--
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0 && indexPath.section == 0) {
        DLog(@"添加配送地址");
        return;
    }
    switch (indexPath.row) {
        case 1:
            DLog(@"配送方式");
            break;
        case 2:
            DLog(@"配送时间");
            break;
        case 3:
            DLog(@"可用积分0");
            break;
            
        default:
            break;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10.0;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
